package com.huxin.servelt;

import com.huxin.pojo.BookHisInfo;
import com.huxin.pojo.Bookreservation;
import com.huxin.pojo.BooksInfo;
import com.huxin.pojo.UserInfo;
import com.huxin.service.Userservice;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.List;

/**
 * 借书
 */
@WebServlet("/user/borrowbook")
public class UserborrowbooksBooksServlet extends HttpServlet {
    private Userservice userservice=new Userservice();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        boolean f=true;//定义一个变量
        //获取书本id
        Integer bid = Integer.valueOf(req.getParameter("bid"));
        //获取用户id todo
        Integer userId = (Integer) req.getSession().getAttribute("userId");
        //查询用户可借阅数量是否大于0
        UserInfo userInfo = userservice.selectById(userId);
        if (userInfo.getMax_num()>0){//有预约借书的资格
            //查询所有的数据
            List<BookHisInfo> bookHisInfos = userservice.selectAll();
            List<Bookreservation> bookreservations=userservice.selectAllreservation();
            //已经借用过或者预约过不能借用
            for (BookHisInfo b : bookHisInfos) {
                if (bid==b.getBid()&&userId==b.getAid()&&b.getStatus()==1){
                    f=false;
                    return;
                }
            }
            for (Bookreservation b : bookreservations) {
                if (bid==b.getBid()&&userId==b.getAid()&&b.getStatus()==1){
                    f=false;
                    return;
                }
            }
            if (f){//
                //根据书本id查询图书数据
                BooksInfo booksInfo=userservice.selectBookById(bid);
                //将数据存入借阅历史表
                Bookreservation bookreservation = Bookreservation.builder()
                        .bid(booksInfo.getBid())//书本id
                        .book_name(booksInfo.getName())//书本名
                        .admin_name(userInfo.getName())//用户名
                        .aid(userInfo.getAid())
                        .card(booksInfo.getCard())
                        .username(userInfo.getUsername())
                        .status(1)
                        .begin_time(new Date(System.currentTimeMillis()))//借书的日期
                        .end_time(new Date(System.currentTimeMillis()+1000*60*60*24*(userInfo.getLend_num())))//还书的日期 在借书的日期上往后推用户的最长借书时间
                        .build();
                System.out.println(bookreservation);
                //在预约书籍表内加入数据
                int i=userservice.insertReservebook(bookreservation);
                //这是减少书籍的操作
                int j=userservice.delectbookinfo(bid);
                if (i==1&&j==1){
                    resp.getWriter().write("1");//预约成功
//                    //借书成功，减少用户最大借书数量
//                    int i1=userservice.decreaseUserNum(userId);
//                    //图书数量减少
//                    int i2=userservice.decreaseBookNum(booksInfo.getBid());
//                    //增加用户借书总数 最佳读者排行要用
//                    int i3=userservice.addFrequency(userId);
//                    //增加该书籍被借阅次数
//                    userservice.addBookHisCard(bid);
//                    if (i1==1&&i2==1){
//                        resp.getWriter().write("1");//借书成功
//                    }
//                      else {
//                        resp.getWriter().write("2");//借书失败
//                    }
                }
                else resp.getWriter().write("2");//预约失败
            }
        }else {//没有借书的资格
            resp.getWriter().write("0");//没有预约资格
        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }




}
